//
//  NotificationView.swift
//  TestDemo
//
//  Created by ayin on 2019/2/26.
//  Copyright © 2019 aYin. All rights reserved.
//  通知提示view

import UIKit

class NotificationSwitchManager: NSObject {
    static let shared = NotificationSwitchManager()
    
    func isUserNotificationEnable() -> Bool {
        if #available(iOS 8.0, *) {
            let type = UIApplication.shared.currentUserNotificationSettings?.types
            if Int(type!.rawValue) == 0 {
                return false
            }else {
                return true
            }
        }
        return true
    }
    
    /// 跳转到系统设置
    func jumpToAlbumSettingPage() {
        if let url = URL(string: UIApplication.openSettingsURLString),
            UIApplication.shared.canOpenURL(url) {
        }
    }
}


class NotificationView: UIView {
    //MARK: - 声明属性
    lazy var notiView:UIView = {
        let notiView = UIView()
        notiView.backgroundColor = UIColor.lightGray
        return notiView
    }()
    
    lazy var titleLB:UILabel = {
        let titleLB = UILabel.makeLabel(font: UIFont.systemFont(ofSize: 11), text: "", textColor: UIColor.black, numberLine: 1, alignment: .left)
        return titleLB
    }()
    
    lazy var openBtn:UIButton = {
        let openBtn = UIButton.makeButton(font: UIFont.systemFont(ofSize: 11), title: "开启", titleColor: UIColor.black, image: nil, bgColor: UIColor.white)
        openBtn.addTarget(self, action: #selector(openBtnAction(_:)), for: .touchUpInside)
        return openBtn
    }()
    
    var title:String = ""
    
    //MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 搭建UI
    func setupUI() {
        self.addSubview(notiView)
        notiView.addSubview(titleLB)
        notiView.addSubview(openBtn)
        
        notiView.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(10)
            make.right.equalToSuperview().offset(-10)
        }
        
        titleLB.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(10)
        }
        
        openBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(40)
            make.height.equalTo(20)
            make.centerY.equalToSuperview()
        }
        
        titleLB.text = title
    }
    
    //MARK: - 事件
    @objc func openBtnAction(_ btn:UIButton) {
        NotificationSwitchManager.shared.jumpToAlbumSettingPage()
    }
}
